package ch.elektroplan.cmueller.logikModulGenerator.presentation.subs;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.elektroplan.cmueller.logikModulGenerator.business.elements.HS_Information;

public class HS_informationPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Inputfield inputfield = new Inputfield();
	private HS_Information hs_Information;

	public HS_informationPanel(HS_Information hs_Information) {

		this.hs_Information = hs_Information;
		this.setLayout(new BorderLayout());

		this.add(inputfield, BorderLayout.NORTH);

	}

	private class Inputfield extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel lb_name = new JLabel("name");
		JLabel lb_folder = new JLabel("folder");
		JLabel lb_remanent = new JLabel("remanent");
		JLabel lb_comment = new JLabel("Kommentar");

		JTextField tf_name = new JTextField("name");
		JTextField tf_folder = new JTextField("ordner");
		JTextField tf_comment = new JTextField("Kommentar");

		String[] options_remanent = { "Nein", "Ja" };
		JComboBox cb_remanent = new JComboBox(options_remanent);

		private Inputfield() {

			setLayout(new GridLayout(2, 4));
			add(lb_name);
			add(lb_folder);
			add(lb_remanent);
			add(lb_comment);
			tf_folder.addFocusListener(new LostFocusListener(tf_folder));
			tf_name.addFocusListener(new LostFocusListener(tf_name));
			cb_remanent.addFocusListener(new LostFocusListener(cb_remanent));
			tf_comment.addFocusListener(new LostFocusListener(tf_comment));
			add(tf_name);
			add(tf_folder);
			add(cb_remanent);
			add(tf_comment);

		}

		private class LostFocusListener implements FocusListener {

			private JComponent jComponent;

			public LostFocusListener(JComponent jComponent) {
				this.jComponent = jComponent;
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub

				if (jComponent.equals(tf_name)) {
					JTextField tf = (JTextField) jComponent;
					hs_Information.setName(tf.getText());
				}
				if (jComponent.equals(tf_folder)) {
					JTextField tf = (JTextField) jComponent;
					hs_Information.setFolder(tf.getText());
				}
				if (jComponent.equals(tf_comment)) {
					JTextField tf = (JTextField) jComponent;
					hs_Information.setComment(tf.getText());
				}
				if (jComponent.equals(cb_remanent)) {
					JComboBox cb = (JComboBox) jComponent;
					hs_Information.setRemanent(cb.getSelectedIndex());
				}

			}

		}

	}

}
